package cn.sjtu.java_util_concurrent.lock;

/**
 * @author syo 2319615164@qq.com
 * @description 三个线程从0 - 100打印
 * @date 2024/6/12
 * @Copyright syo
 */
public class Print100 {

    public static volatile int count = 0;
    public static void main(String[] args) {
        new Thread(() ->{
            while (count <= 100) {
                synchronized (Print100.class) {
                    while (count % 3 != 0) {
                        try {
                            Print100.class.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }

                    if (count <= 100) {
                        System.out.println(count++);
                    }
                    Print100.class.notifyAll();
                }
            }
        }).start();

        new Thread(() ->{
            while (count <= 100) {
                synchronized (Print100.class) {
                    while (count % 3 != 1) {
                        try {
                            Print100.class.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }

                    if (count <= 100) {
                        System.out.println(count++);
                    }
                    Print100.class.notifyAll();
                }
            }
        }).start();

        new Thread(() ->{
            while (count <= 100) {
                synchronized (Print100.class) {
                    while (count % 3 != 2) {
                        try {
                            Print100.class.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }

                    if (count <= 100) {
                        System.out.println(count++);
                    }
                    Print100.class.notifyAll();
                }
            }
        }).start();
    }
}
